/**
 * @file testcases.c
 * @provides testcases
 *
 * $Id: testcases.c 175 2008-01-30 01:18:27Z brylow $
 *
 * Modified by:
 *
 * and
 *
 */
/* Embedded XINU, Copyright (C) 2007.  All rights reserved. */

#include <kernel.h>
#include <device.h>
#include <stdio.h>
#include <uart.h>
#include <proc.h>
#include <queue.h>

void printQueue(queue q)
{
	int head, tail, index;

	head = queuehead(q);
	tail = queuetail(q);

	index = queuetab[head].next;

	kprintf ("Contents of Queue\r\n");
	kprintf ("Head [Previous: %d Next: %d]\r\n", queuetab[head].prev, queuetab[head].next);
	kprintf ("Tail [Previous: %d Next: %d]\r\n", queuetab[tail].prev, queuetab[tail].next);

	while (index != tail)
	{
		kprintf ("[Previous: %3d Next: %3d Key %3d]\r\n", queuetab[index].prev, queuetab[index].next, queuetab[index].key);
		index = queuetab[index].next;
	}

	kprintf ("\r\n");
}
 
void printpid(int times)
{
	int i = 0;
	   
	enable();
	for (i = 0; i < times; i++)
	{
		kprintf("This is process %d\r\n", currpid);
		resched();
	}
}

/**
 * testcases - called after initialization completes to test things.
 */
void testcases(void)
{
	int c;
	
	/*queue testqueue;
	testqueue = newqueue();

	prioritize(1, testqueue, 2);

	prioritize(2, testqueue, 1);

	prioritize(3, testqueue, 2);

	printQueue(testqueue);*/

	
	kprintf("0) Test priority scheduling\r\n");

	kprintf("===TEST BEGIN===\r\n");

	// TODO: Test your operating system!

	c = kgetc();
	switch(c)
	{
	case	'0':
		ready(create((void *)printpid, INITSTK, 2, "PRINTER-A", 1, 5), 0);
		ready(create((void *)printpid, INITSTK, 5, "PRINTER-B", 1, 5), 0);
		ready(create((void *)printpid, INITSTK, 10, "PRINTER-C", 1, 5), 0);
		ready(create((void *)printpid, INITSTK, 5, "PRINTER-D", 1, 5), 0);
		break;

	default:
		ready(create((void *)printpid, INITSTK, 2, "PRINTER-A", 1, 5), 0);
		kprintf ("default\r\n");
		break;
	}

	printQueue(readylist);

	while (numproc > 2) { resched(); }
	
	kprintf("\r\n===TEST END===\r\n");
	return;
}
